﻿using ExcelAutomator.Configuration;
using Microsoft.Office.Interop.Excel;

namespace ExcelAutomator.ParameterPattern
{
	[ParameterPatternResolver(PatternIdDefinition)]
	internal class LastUsedRowResolver : ExcelParameterPatternResolverBase
	{
		public const string PatternIdDefinition = "%lastUsedRow";

		public override string PatternId
		{
			get { return PatternIdDefinition; }
		}

		public override string OnResolve(string value, ExecutionContextConfiguration contextConfiguration, ExecutionContext context)
		{
			Worksheet worksheet;
			if (context.TryGetWorksheet(value, contextConfiguration, out worksheet))
			{
				if (worksheet != null)
				{
					return worksheet.GetLastRowWithData().ToString();
				}
			}
			
			return value;
		}
	}
}
